package net.lukecloud.xile;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Retrofit;
import retrofit2.converter.fastjson.FastJsonConverterFactory;

/**
 * Created by jmy on 2017/7/21.
 */

public class App extends Application {
    public  Retrofit retrofit;
    public String id_token;
    public String show_name;
    public String imageUrl;
    private static final String TAG = "AppInit";
    @Override
    public void onCreate(){
        super.onCreate();

        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor()//log，统一的header等
        {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException
            {
                if(id_token!=null&&id_token.length()>190){
                    Request original = chain.request();
                    Request.Builder requestBuilder = original.newBuilder()
                            .addHeader("Authorization", "Bearer "+id_token);
                    Request request = requestBuilder.build();
                    return chain.proceed(request);
                }else{
                    return chain.proceed(chain.request());
                }
            }
        }).build();
         retrofit = new Retrofit.Builder()
//                .baseUrl("http://192.168.0.116:8080/")
                .baseUrl("https://lukecloud.net/")
                 .callFactory(client)
                .addConverterFactory(FastJsonConverterFactory.create())
                .build();

        SharedPreferences account_info = getSharedPreferences("ACCOUNT_INFO", 0);
        id_token = account_info.getString("id_token", "");
        show_name = account_info.getString("show_name", "");
        imageUrl = account_info.getString("imageUrl", "");

        initCloudChannel(this);

    }

    public  void cancel(){
        SharedPreferences account_info = getSharedPreferences("ACCOUNT_INFO", 0);
        account_info.edit().clear().commit();
    }


    /**
     * 初始化云推送通道
     * @param applicationContext
     */
    private void initCloudChannel(Context applicationContext) {
        PushServiceFactory.init(applicationContext);
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        pushService.getDeviceId();
        pushService.getUTDeviceId();

        pushService.register(applicationContext, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                Log.d(TAG, "init cloudchannel success");
            }
            @Override
            public void onFailed(String errorCode, String errorMessage) {
                Log.d(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
            }
        });
    }

}
